﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Readify.Management.Automation.GoGrid
{
    public class GoGridServer
    {
        internal static GoGridServer CreateFromXmlNode(XmlNode objectElementNode)
        {
            GoGridServer output = new GoGridServer();
            output.ID = GoGridHelper.GetAttributeElementValueFromXmlNodeAsNullableInt32(objectElementNode, "id");
            output.Name = GoGridHelper.GetAttributeElementValueFromXmlNodeAsString(objectElementNode, "name");
            output.Description = GoGridHelper.GetAttributeElementValueFromXmlNodeAsString(objectElementNode, "description");

            XmlNode ipAttributeXmlNode = GoGridHelper.GetAttributeElementFromXmlNodeAsFirstChildXmlNode(objectElementNode, "ip");
            output.IPAddress = GoGridIPAddress.CreateFromXmlNode(ipAttributeXmlNode);

            XmlNode memoryAttributeXmlNode = GoGridHelper.GetAttributeElementFromXmlNodeAsFirstChildXmlNode(objectElementNode, "ram");
            output.Memory = GoGridOption.CreateFromXmlNode(memoryAttributeXmlNode);

            XmlNode imageAttributeXmlNode = GoGridHelper.GetAttributeElementFromXmlNodeAsFirstChildXmlNode(objectElementNode, "image");
            output.Image = GoGridImage.CreateFromXmlNode(imageAttributeXmlNode);

            XmlNode stateAttributeXmlNode = GoGridHelper.GetAttributeElementFromXmlNodeAsFirstChildXmlNode(objectElementNode, "state");
            output.State = GoGridOption.CreateFromXmlNode(stateAttributeXmlNode);

            XmlNode typeAttributeXmlNode = GoGridHelper.GetAttributeElementFromXmlNodeAsFirstChildXmlNode(objectElementNode, "type");
            output.Type = GoGridOption.CreateFromXmlNode(typeAttributeXmlNode);

            XmlNode operatingSystemAttributeXmlNode = GoGridHelper.GetAttributeElementFromXmlNodeAsFirstChildXmlNode(objectElementNode, "os");
            output.OperatingSystem = GoGridOption.CreateFromXmlNode(operatingSystemAttributeXmlNode);

            return output;
        }

        public int? ID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public GoGridIPAddress IPAddress { get; set; }
        public GoGridOption Memory { get; set; }
        public GoGridImage Image { get; set; }
        public GoGridOption State { get; set; }
        public GoGridOption Type { get; set; }
        public GoGridOption OperatingSystem { get; set; }

        public override string ToString()
        {
            return string.Format("{0} ({1})", this.Name, this.IPAddress);
        }
    }
}
